package top.lidee.module.backendApi.twice.popularize.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.dto.AccountInfo;
import top.lidee.common.service.twice.management.BmsManagerService;
import top.lidee.common.service.twice.popularize.PtpBeesInfoService;
import top.lidee.common.util.TokenUtil;
import top.lidee.framework.enums.CommonExceptionEnum;
import top.lidee.framework.result.PageUtils;
import top.lidee.framework.result.ResponseResult;
import top.lidee.repository.model.twice.pojo.popularize.PtpBeesInfoEntity;
import top.lidee.repository.model.twice.vo.popularize.PtpBeesInfoSaveVo;
import top.lidee.repository.model.twice.vo.popularize.PtpBeesInfoUpdateVo;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 推广人/小蜜蜂人员表
 *
 * @author zhangzhan
 * @email 52194440@qq.com
 * @date 2024-09-23 14:39:33
 */
@Api("推广人/小蜜蜂人员表")
@RestController
@RequestMapping("/backendApi/ptpbeesinfo")
public class PtpBeesInfoController {

    @Autowired
    private PtpBeesInfoService ptpBeesInfoService;

    @Autowired
    private BmsManagerService bmsManagerService;


    /**
     * 条件查询
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "推广人/小蜜蜂人员表-条件查询")
    public ResponseResult list(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        Map<String, Object> params=new HashMap<>();
        params.put("nickname",request.getParameter("nickname"));
        params.put("phone",request.getParameter("phone"));
        params.put("managerName",request.getParameter("managerName"));
        params.put("province",request.getParameter("province"));
        params.put("city",request.getParameter("city"));
        params.put("county",request.getParameter("county"));
        PageUtils page = ptpBeesInfoService.queryPage(params, accountInfo);

        return ResponseResult.okResultPage("page", page);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/save/bees", method = RequestMethod.POST)
    @ApiOperation(value = "推广人/小蜜蜂人员表-新增")
    public ResponseResult saveBees(HttpServletRequest request, @RequestBody @Valid PtpBeesInfoSaveVo vo) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = ptpBeesInfoService.saveBees(vo, accountInfo);

        return responseResult;
    }

    /**
     * 根据id查询
     */
    @RequestMapping(value = "/bees/{beesId}", method = RequestMethod.GET)
    @ApiOperation(value = "推广人/小蜜蜂人员表-根据id查询")
    public ResponseResult getBeesById(HttpServletRequest request, @PathVariable("beesId") Integer beesId) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = ptpBeesInfoService.getBeesById(beesId, accountInfo);

        return responseResult;
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/updateInfo", method = RequestMethod.PUT)
    @ApiOperation(value = "推广人/小蜜蜂人员表-修改")
    public ResponseResult updateInfo(HttpServletRequest request, @RequestBody PtpBeesInfoUpdateVo vo) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = ptpBeesInfoService.updateInfo(vo, accountInfo);

        return responseResult;
    }

    /**
     * 修改状态
     */
    @RequestMapping(value = "/updateStatusById/{beesId}", method = RequestMethod.PUT)
    @ApiOperation(value = "推广人/小蜜蜂人员表-修改状态")
    public ResponseResult updateStatus(HttpServletRequest request, @PathVariable("beesId") Integer beesId) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = ptpBeesInfoService.updateStatusById(beesId, accountInfo);

        return responseResult;
    }


    /**
     * 删除小蜜蜂
     */
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.DELETE)
    @ApiOperation(value = "推广人/小蜜蜂人员表-删除小蜜蜂")
    public ResponseResult deleteByIds(HttpServletRequest request, @RequestBody Integer[] beesIds) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = ptpBeesInfoService.deleteByIds(beesIds, accountInfo);

        return responseResult;
    }

    /**
     * 获取小蜜蜂的所有服务商数据
     */
    @RequestMapping(value = "/getManagers", method = RequestMethod.GET)
    @ApiOperation(value = "推广人/小蜜蜂人员表-删除小蜜蜂")
    public ResponseResult getAllManagers(HttpServletRequest request ) {
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = bmsManagerService.getAllManagesInfo();
        return responseResult;
    }


}
